package com.ayu.wxgzh.ayucyy.service;

import com.ayu.wxgzh.ayucyy.entity.Article;
import com.ayu.wxgzh.ayucyy.entity.EduCourse;
import com.ayu.wxgzh.ayucyy.mapper.EduCourseMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 徐林玉
 * @Description VodService
 * @Date 2022/9/1 17:00
 * @Version 1.0
 */
@Service
public class VodService {

    @Value("${my.ip:}")
    private String ipAddress;


    private final EduCourseMapper eduCourseMapper;

    @Autowired
    public VodService(EduCourseMapper eduCourseMapper) {
        this.eduCourseMapper = eduCourseMapper;
    }


    public Article getArticleByText(String text) {

        QueryWrapper<String> queryWrapper = new QueryWrapper<>();

        queryWrapper.like(StringUtils.isNotEmpty(text), "title", text);

        List<EduCourse> videoList = eduCourseMapper.getVideoIdByText(queryWrapper);

        Article videoArticle = getVideoArticle(videoList);

        return videoArticle;
    }


    private Article getVideoArticle(List<EduCourse> videoList) {

        Article article = null;

        if (CollectionUtils.isNotEmpty(videoList)) {


            article = new Article();

            EduCourse topOne = videoList.get(0);

            article.setPicUrl(topOne.getCover());

            article.setDescription(topOne.getDescription());

            article.setTitle(topOne.getTitle());

            if(videoList.size()==1){

                article.setUrl(ipAddress+"/"+ topOne.getId());

            }else{
                article.setUrl(ipAddress);
            }
        }

        return article;
    }
}
